package com.wzy.authority.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.wzy.authority.base.dto.SysPermissionDto;
import com.wzy.authority.base.result.Results;
import com.wzy.authority.domain.SysPermission;
import com.wzy.authority.mapper.SysPermissionMapper;
import com.wzy.authority.service.SysPermissionService;
import com.wzy.authority.utils.TreeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: authorityManagement
 * @description: 用户菜单实现类
 * @author: 1
 * @create: 2020-03-20 14:09
 **/

@Slf4j
@Service
public class SysPermissionServiceImpl implements SysPermissionService {

    @Resource
    private SysPermissionMapper sysPermissionMapper;


    @Override
    public Results<JSONArray> getAllMenuList() {
        List<SysPermission> allMenuList = sysPermissionMapper.getAllMenuList();
        JSONArray array = new JSONArray();
        TreeUtils.setPermissionsTree(0, allMenuList, array);
        return Results.success("获取数据成功", array);
    }

    @Override
    public Results<List<SysPermission>> getAllMenuListById(Integer roleId) {
        List<SysPermission> sysPermissions = sysPermissionMapper.selectByRoleId(roleId);
        return Results.success("获取权限列表成功",sysPermissions);
    }

    @Override
    public Results<List<SysPermission>> getAllPermission() {
        List<SysPermission> sysPermissions = sysPermissionMapper.getAllMenuList();
        return Results.success("获取权限列表成功",sysPermissions);
    }

    @Override
    public SysPermission getPermissionById(Integer id) {
        return sysPermissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public Results<Void> savePermission(SysPermission sysPermission) {
        return sysPermissionMapper.insert(sysPermission) >=0 ? Results.success() : Results.failure();
    }

    @Override
    public Results<Void> editPermission(SysPermission sysPermission) {
        return sysPermissionMapper.updateByPrimaryKeySelective(sysPermission)>=0 ? Results.success() : Results.failure();
    }

    @Override
    public Results<Void> deletePermission(Integer permissionId) {
        //删除id为permissionId的并且删除所有的下级菜单,所以删除parentId为permissionId的数据
        return sysPermissionMapper.deleteByPermissionId(permissionId) >= 0 ? Results.success() : Results.failure();
    }

    @Override
    public Results<JSONArray> getAllMenuListByUserId(Integer userId) {
        List<SysPermission> allMenuList = sysPermissionMapper.getAllMenuListByUserId(userId);
        JSONArray array = new JSONArray();
        allMenuList = allMenuList.stream().filter(menu -> {
          return menu.getType().equals(1);
        }).collect(Collectors.toList());
        TreeUtils.setPermissionsTree(0, allMenuList, array);
        return Results.success("获取数据成功", array);
    }

    @Override
    public List<SysPermission> listByUserId(Integer userId) {
        return sysPermissionMapper.getAllMenuListByUserId(userId);
    }
}
